package com.eveinfo.display.skills;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.eveinfo.R;
import com.eveinfo.api.character.sheet.ApiSheet;
import com.eveinfo.api.utils.skilllist.ApiSkill;
import com.eveinfo.display.LayoutUtils;

public class SkillsViewRenderer extends TableLayout {

	private TextView lblName;
	private TextView lblRank;
	private TextView lblLevel;
	private ImageView lblIcone;
	private Context context;

	public SkillsViewRenderer(Context context) {
		super(context);
		this.context = context;
		createUI();
	}

	/** create the UI components */
	private void createUI() {
		setColumnShrinkable(1, true);
		setColumnStretchable(1, true);
		
		TableRow rowTop = new TableRow(context);
		rowTop.setPadding(0, 0, 10, 0);
		LayoutUtils.Layout.WidthFill_HeightWrap.applyTableLayoutParams(rowTop);

		TableRow rowBottom = new TableRow(context);
		rowBottom.setPadding(0, 0, 10, 0);
		LayoutUtils.Layout.WidthFill_HeightWrap.applyTableLayoutParams(rowBottom);

		lblName = new TextView(context);
		lblName.setTextColor(Color.WHITE);
		LayoutUtils.Layout.WidthWrap_HeightWrap.applyTableRowParams(lblName);

		lblLevel = new TextView(context);
		lblLevel.setTextColor(Color.WHITE);
		lblLevel.setGravity(Gravity.RIGHT);
		lblLevel.setPadding(0, 0, 10, 0);
		LayoutUtils.Layout.WidthFill_HeightWrap.applyTableRowParams(lblLevel);

		lblRank = new TextView(context);
		lblRank.setTextColor(Color.WHITE);
		lblRank.setTextSize(11);
		lblRank.setPadding(0, 0, 0, 6);
		LayoutUtils.Layout.WidthWrap_HeightWrap.applyTableRowParams(lblRank);

		
		LinearLayout layout = new LinearLayout(context);
		layout.setGravity(Gravity.RIGHT);
		lblIcone = new ImageView(context);
		lblIcone.setPadding(0, 0, 10, 0);
		layout.addView(lblIcone);
		
		LayoutUtils.Layout.WidthWrap_HeightWrap.applyTableRowParams(lblIcone);

		rowTop.addView(lblName);
		rowTop.addView(lblLevel);

		rowBottom.addView(lblRank);
		rowBottom.addView(layout);

		addView(rowTop);
		addView(rowBottom);

	}

	public void displaySkill(ApiSkill skill, ApiSheet sheet) {

		int skillLevel = sheet.getSkillLevel(skill.getTypeID());
		
		lblName.setText(skill.getTypeName());
		if(skillLevel == -1){
			lblLevel.setText("Unknow");
		}else{
			lblLevel.setText("Level " + skillLevel);
		}
		lblRank.setText("(Rank " + skill.getRank() + ")");
		switch (skillLevel) {
		case 1:
			lblIcone.setImageResource(R.drawable.skill_1);
			break;
		case 2:
			lblIcone.setImageResource(R.drawable.skill_2);
			break;
		case 3:
			lblIcone.setImageResource(R.drawable.skill_3);
			break;
		case 4:
			lblIcone.setImageResource(R.drawable.skill_4);
			break;
		case 5:
			lblIcone.setImageResource(R.drawable.skill_5);
			break;
		default:
			lblIcone.setImageResource(R.drawable.skill_0);
			break;
		}
		return;
	}
}
